<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['classroomsNumber',validatorRules.classroomsNumber]" placeholder="请输入教室编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['classroomsName', validatorRules.classroomsName]" placeholder="请输入教室名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <j-dict-select-tag :triggerChange="true" dictCode="msgSendStatus" v-decorator="[ 'esSendStatus', {}]" placeholder="请选择发送状态">-->

              <j-dict-select-tag  placeholder="请选择教室类型"  v-decorator="['classroomsType', validatorRules.classroomsType]" :trigger-change="true" dictCode="classrooms_type" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室面积(平方米)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['classroomsArea', validatorRules.classroomsArea]" placeholder="请输入教室面积"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室最小容量(人)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['minCapacity', validatorRules.minCapacity]" placeholder="请输入教室最小容量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室最大容量(人)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['classroomsCapacity', validatorRules.classroomsCapacity]" placeholder="请输入教室最大容量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室租金(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['classroomsRental', validatorRules.classroomsRental]" placeholder="请输入教室租金"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="教室照片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['classroomsPhoto']"></j-image-upload>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="教室物资" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="classroomsPropertyTable.loading"
            :columns="classroomsPropertyTable.columns"
            :dataSource="classroomsPropertyTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'ClassroomsModal',
    mixins: [JEditableTableMixin],
    components: {
      JImageUpload,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 0,
        validatorRules: {
          classroomsNumber: {
            rules: [
              { validator: (rule, value, callback) => validateDuplicateValue('classrooms', 'classrooms_number', value, this.model.id, callback)},
              { required: true, message: '请输入教室编号!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
            ]
          },
          classroomsName: {
            rules: [
              { required: true, message: '请输入教室名称!'},
            ]
          },
          classroomsType: {
            rules: [
              { required: true, message: '请输入教室类型!'},
            ]
          },
          classroomsArea: {
            rules: [
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
              { required: true, message: '请输入教室面积!'},
            ]
          },
          minCapacity:{
            rules: [
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
              { required: true, message: '请输入容积!'},
            ]
          },
          classroomsCapacity: {
            rules: [
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
              { required: true, message: '请输入容积!'},
            ]
          },
          classroomsRental: {
            rules: [
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
              { required: true, message: '请输入金额!'},
            ]
          },
        },

        refKeys: ['classroomsProperty', ],
        tableKeys:['classroomsProperty', ],
        activeKey: 'classroomsProperty',
        // classrooms_property
        classroomsPropertyTable: {
          loading: false,
          dataSource: [],
          columns: [
            // {
            //   title: '教室id',
            //   key: 'classroomsId',
            //   type: FormTypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue: '',
            // },
            {
              title: '物资名称 ',
              key: 'propertyId',
              type: FormTypes.sel_search,
              dictCode:"property,property_name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '物资数量',
              key: 'propertyNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/classrooms/add",
          edit: "/classrooms/edit",
          classroomsProperty: {
            list: '/classrooms/queryClassroomsPropertyByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'classroomsNumber','classroomsName','classroomsType','classroomsStatus','rentStatus','classroomsArea','classroomsCapacity','minCapacity','classroomsRental','propertyId','classroomsPhoto')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.classroomsProperty.list, params, this.classroomsPropertyTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          classroomsPropertyList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'classroomsNumber','classroomsName','classroomsType','classroomsStatus','rentStatus','classroomsArea','classroomsCapacity','minCapacity','classroomsRental','propertyId','classroomsPhoto'))
     },

    }
  }
</script>

<style scoped>
</style>